package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import com.atguigu.jxc.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ClassName: CustomerServiceImpl
 * Package: com.atguigu.jxc.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/21 - 15:38
 * @Version: v1.0
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;
    @Autowired
    private LogService logService;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        Map<String, Object> map = new HashMap<>();
        int total = customerDao.getCustomerCount(customerName);
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Customer> roles = customerDao.getCustomerList(offSet, rows, customerName);
        logService.save(new Log(Log.SELECT_ACTION, "分页查询客户信息"));
        map.put("total", total);
        map.put("rows", roles);
        return map;
    }

    @Override
    public ServiceVO save(Customer customer) {
        // 供应商ID为空，则为新增操作，否则为修改操作
        if (customer.getCustomerId() == null) {
            Customer exCustomer = customerDao.findCustomerByName(customer.getCustomerName());
            if (exCustomer != null) {
                return new ServiceVO<>(ErrorCode.CUSTOMER_EXIST_CODE, ErrorCode.CUSTOMER_EXIST_MESS);
            }
            logService.save(new Log(Log.INSERT_ACTION, "新增客户:" + customer.getCustomerName()));
            customerDao.insertCustomer(customer);
        } else {
            logService.save(new Log(Log.UPDATE_ACTION, "修改客户:" + customer.getCustomerName()));
            customerDao.updateCustomer(customer, customer.getCustomerId());
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String customerIds) {
        try {
            // 去掉前缀 ids=
            String cleanedCustomerIds = customerIds.replaceFirst("^ids=", "");
            // 解码 URL 编码的字符串
            String decodedCustomerIds = URLDecoder.decode(cleanedCustomerIds, StandardCharsets.UTF_8.toString());
            // 分割字符串
            String[] customerIdArr = decodedCustomerIds.split(",");
            // 转换为整数列表
            List<Integer> customerIdList = Arrays.stream(customerIdArr).map(Integer::parseInt).collect(Collectors.toList());
            for (Integer customerId : customerIdList) {
                // 校验供应商是否存在
                Customer customer = customerDao.getCustomerById(customerId);
                if (customer == null) {
                    return new ServiceVO<>(ErrorCode.CUSTOMER_DEL_ERROR_CODE, ErrorCode.CUSTOMER_DEL_ERROR_MESS);
                }
                // 删除供应商
                customerDao.deleteCustomer(customerId);
                logService.save(new Log(Log.DELETE_ACTION, "删除客户:" + customerId));
            }
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (UnsupportedEncodingException e) {
            // 处理解码异常
            return new ServiceVO<>(ErrorCode.DECODE_ERROR_CODE, ErrorCode.DECODE_ERROR_MESS);
        } catch (NumberFormatException e) {
            // 处理转换异常
            return new ServiceVO<>(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }
}
